package com.service.impl;

import com.entity.Client;
import com.entity.Order;
import com.entity.PageBean;
import com.exception.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ClientMapper;
import com.service.ClientService;
import com.vo.ClientVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public Client getByCid(Integer cid) {
        return clientMapper.selectByCid(cid);
    }

    @Override
    public void modify(Client client) {
        clientMapper.update(client);
    }

    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void remove(Integer cid) {
        clientMapper.delete(1, cid);
        clientMapper.updateState("未启用", cid);
    }

    @Override
    public PageBean<Client> getAll(Integer pageIndex, Integer pageSize, ClientVO clientVO) {
        System.out.println(clientVO);
        PageHelper.startPage(pageIndex, pageSize);

        List<Client> list= clientMapper.selectAll(clientVO);

        PageInfo<Client> pageInfo = new PageInfo<>(list);

        PageBean<Client> pageBean = new PageBean<>(
                pageIndex,
                pageSize,
                pageInfo.getPages(),
                (int)pageInfo.getTotal(),
                pageInfo.getList()
        );

        return pageBean;
    }

    @Override
    public Client getByCname(String cname) {
        Client client = clientMapper.selectClientByCname(cname);
        if(client == null){
            throw new ClientException("首次订购的客户");
        }
        return client;
    }
}
